﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace editor.app
{
    /// <summary>
    /// RKFAnimImage.xaml 的交互逻辑
    /// </summary>
    public partial class RKFAnimImage : UserControl
    {
        comm.objst.RPack _rpack = null;
        comm.objst.RKeyFrameAnim.Image _image = null;

        public Action OnCloseCallback = null;

        public RKFAnimImage()
        {
            InitializeComponent();

            btnClose.Click += new RoutedEventHandler(btnClose_Click);
            btnClear.Click += new RoutedEventHandler(btnClear_Click);
        }

        void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (OnCloseCallback != null)
            {
                OnCloseCallback();
            }
        }
        void btnClear_Click(object sender, RoutedEventArgs e)
        {
            _image.FrameList.Clear();

            Fill();
        }
        void gridFrame_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var grid = (Grid)sender;
            var ix = (int)grid.Tag;
            _image.FrameList.RemoveAt(ix);
            Fill();
        }

        public void Load(comm.objst.RPack rpack, string kfanimResKey, string imageKey)
        {
            var kfanim = rpack.GetResource<comm.objst.RKeyFrameAnim>(kfanimResKey);

            _rpack = rpack;
            _image = kfanim.ImageDic[imageKey];

            Fill();

            imageView.OnClickFrameListCallback = (frames) =>
            {
                foreach (var frameIndex in frames)
                {
                    _image.FrameList.Add(frameIndex);
                }
                Fill();
            };
            imageView.OnClickFrameCalback = (frameIndex, x, y) =>
            {
                _image.FrameList.Add(frameIndex);
                Fill();
            };
        }

        void Fill()
        {
            var rimage = _rpack.GetResource<comm.objst.RImage>(_image.ResKey);

            spFrame.Children.Clear();
            var ix = 0;
            foreach (var index in _image.FrameList)
            {
                var grid = new Grid { Background = new SolidColorBrush(Color2.White()), Width = 100, Height = 100, Tag = ix, Margin = new Thickness(4, 0, 0, 0) };
                var frame = rimage != null ? rimage.GetFrame(index) : null;
                grid.Children.Add(new Border { BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Color2.White()) });
                if (frame != null)
                {
                    grid.Width = frame.Width;
                    grid.Height = frame.Height;
                    grid.Background = null;
                }
                grid.Children.Add(RImage.GetFrameCanvas(_rpack, _image.ResKey, index));
                grid.Children.Add(new TextBlock { Text = index.ToString(), FontSize = 12, Margin = new Thickness(1, 1, 0, 0), Foreground = new SolidColorBrush(Color2.White(50)) });
                grid.Children.Add(new TextBlock { Text = index.ToString(), FontSize = 12 });
                grid.MouseLeftButtonDown += new MouseButtonEventHandler(gridFrame_MouseLeftButtonDown);
                spFrame.Children.Add(grid);
                ix++;
            }

            if (rimage != null)
            {
                imageView.Load(_rpack, _image.ResKey, _image.FrameList.ToArray());
            }
        }
    }
}
